<?php

/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2018/7/30
 * Time: 15:28
 */
class websocket
{
    const HOST = "0.0.0.0";
    const PORT = 8813;

    public $server;
    public function __construct()
    {
        $this->server = new swoole_websocket_server(HOST, PORT);

        $this->server->on('open', [$this, 'onOpen']);
        $this->server->on('message', [$this, 'onMessage']);
        $this->server->on('close', [$this, 'onClose']);

        $this->server->start();
    }

    public function onOpen (swoole_websocket_server $server, $request) {
        echo "server: handshake success with fd{$request->fd}\n";
    }

    public function onMessage(swoole_websocket_server $server, $frame){
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    }

    public function onClose($ser, $fd){
        echo "client {$fd} closed\n";
    }
}

$obj = new websocket();